﻿<!--#include file="../Inc/easp/easp.asp"-->
<!--#include file="../Inc/conn1.asp"-->
<!--#include file="../inc/checklogin.asp"-->
<!--#include file="../inc/easyide.asp"-->
<% CheckLogin("index.asp") %>
<%
	If Session("Authority")<>"0" Then
		Response.Write "<script type=""text/javascript"">top.location.href='../main.asp';</script>"
	end if				
%>
<%
  '创建新用户
  dim username,password,authority,szSelect,selectArr,gid
  username  = Request.Form("username")'用户名
  password  = Request.Form("password")'密码
  authority = Request.Form("radio")'权限
  gid = Request.Form("GroupList")
  szSelect  = Trim(Request.Form("CountryList"))
  selectArr = Split(szSelect, ",")'选中的国家ID 
  
  dim rs,canFlag
  canFlag = true
  set rs = Easp.db.GetRecordBySql("select * from [user] where Name='" & username & "'")
  if Easp.IsN(rs) then  
  	canFlag = true    
  else
  	if rs.recordcount = 0 then
  		canFlag = true
  	else
  		canFlag = false
    end if  	
  end if
  
  if canFlag then
    dim result
    result = Easp.db.AddRecord("user", Array("g_id:" & gid, "Name:"&username, "PassWord:"&password, "Authority:"&authority))
    if result<>0 then
  	  'Response.Write "<script type=""text/javascript"">alert('Successfully Add New User!');top.location.href='index.asp';</script>"
    else
  	  Response.Write "<script type=""text/javascript"">alert('Add User Failed!');top.location.href='index.asp';</script>"
    end if
    
    dim newId
    newId = CInt(Easp.db.AutoId("User:ID"))'获取刚加入的记录ID  
    newId = newId - 1
  
    '添加管理国家记录
    if szSelect<>"" then
      dim i
      for i = 0 to ubound(selectArr)        
        call Easp.db.Exec("insert into UserCountry(UserID, CountryID) values(" & newId & "," & selectArr(i) & ")")
      next
    End if   
  
    Response.Write "<script type=""text/javascript"">alert('Successfully Add New User!');top.location.href='index.asp';</script>"
  else
  	Response.Write "<script type=""text/javascript"">alert('The UserName is Exists!');window.history.back(-1);</script>"
  end if
%>